import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api'
// import addresses from '@/api/addresses'
import {lookBanner,lookRestaurant,lookMenuClassification,lookGoods,lookAddress} from '../utils/http/index.js'
import {Banneer} from '../utils/controller/index.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		store: {},
		cart: [],
		orderdetail:'', // 当前订单详情
		orderType: 'takein', // takein 自取 takout 外带
		address:{}, // 用户默认地址
		addresses: [], // 用户地址数据
		member: {}, // 页面个人数据
		order: {}, // 储存订单
		banner:[], // 轮播图
		lookRestaurantlist:[], // 餐厅列表
		Classification:[],//查看菜单所有分类
		lookGoodslist:[], // 查看分类下的详情
		lookList:[],//餐厅数组列表
		tabbarIndex:'',//tabbar 的传参
	},
	getters: {
		isLogin: state => Object.keys(state.member).length > 0	//是否登录
	},
	mutations: {
		SET_ORDER_TYPE(state, type) {
			state.orderType = type
		},
		SET_MEMBER(state, member) {
			state.member = member
		},
		SET_ADDRESSES(state, addresses) {
			state.addresses = addresses
			let a = addresses.filter((item)=>{
				return item.isDefault == 0
			})
			console.log('aaa',a[0])
			state.address = a[0]
		},
		SET_STORE(state, store) {
			state.store = store
		},
		SET_CART(state, cart) {
			state.cart = cart
		},
		REMOVE_CART(state) {
			state.cart = []
		},
		SET_ORDER(state, order) {
			state.order = order
		},
		// 当前的订单详情
		SET_ORDERDETAIL(state,order){
			state.orderdetail = order
		},
		// 轮播图
		SET_BANNER(state,banner){
			state.banner = banner
		},
		// 餐厅列表
		SET_LOOKRESTAURANT(state,lookRestaurantlist){
			state.lookRestaurantlist = lookRestaurantlist
			// 餐厅数组列表
			state.lookList = lookRestaurantlist.map((item,index) => item.address)
		},
		// 查看菜单所有分类
		SET_CLASSSTAURANT(state,Classification){
			state.Classification = Classification
		},
		// 改变tabbar传参
		menustatus(state,tabbarIndex){
			state.tabbarIndex = tabbarIndex
		}
	},
	actions: {
		async getStore({commit},store) {
			commit('SET_STORE', store)
		},
		async getbanner({commit}) {
			const {data} = await lookBanner()
			commit('SET_BANNER',Banneer.banner(data.data))
		},
		async getlookRestaurant({commit}) {
			const {data} = await lookRestaurant()
			commit('SET_LOOKRESTAURANT',Banneer.banner(data.data))
		},
		async getlookMenuClassification({commit},value) {
			const {data} = await lookMenuClassification(value)
			commit('SET_CLASSSTAURANT',Banneer.banner(data.data))
		},
		// 用户获取地址
		async getlookAddress({commit},value) {
			const {data} = await lookAddress({token:value})
			console.log(data)
			commit('SET_ADDRESSES',data.data)
		}
	}
})

export default store